// 电影管理相关接口
import { getFileContext, doPagination, responseStatus, addOneData, setFileContext, getDataByKey, updateOneData, removeOneData } from './util'
import { movieAreaFile, movieTypeFile, movieFile } from './filePath'
import dayjs from 'dayjs'
export default [
  // 获取电影列表
  {
    url: '/api/movie/movieList',
    method: 'get',
    timeout: 200,
    response: ({ query }) => {
      const { name } = query
      const data = getFileContext(movieFile)
      const filterData = data.filter(item => item.name.includes(name))
      const res = doPagination(filterData.reverse(), query.page, query.pageSize)
      return {
        ...responseStatus(),
        data: res,
        total: data.length
      }
    }
  },
  // 电影新增
  {
    url: '/api/movie/movieAdd',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(movieFile)
      const res = getDataByKey(data, body.name, 'name')
      if (res) {
        return {
          ...responseStatus(2),
          msg: '影片已存在'
        }
      }
      body.createTime = dayjs().format('YYYY-MM-DD hh:mm:ss')
      // 增加类型和地区的影片数量
      if (body.areaId) {
        const areas = getFileContext(movieAreaFile)
        const area = getDataByKey(areas, body.areaId, 'id')
        area.count = area.count ? area.count + 1 : 1
        setFileContext(movieAreaFile, areas)
      }
      if (body.typeId) {
        const types = getFileContext(movieTypeFile)
        const type = getDataByKey(types, body.typeId, 'id')
        type.count = type.count ? type.count + 1 : 1
        setFileContext(movieTypeFile, types)
      }
      addOneData(data, body)
      setFileContext(movieFile, data)
      return {
        ...responseStatus()
      }
    }
  },
  {
    url: '/api/movie/movieInfo',
    method: 'get',
    timeout: 200,
    response: ({ query }) => {
      const data = getFileContext(movieFile)
      const res = getDataByKey(data, Number(query.id))
      if (res) {
        return {
          ...responseStatus(),
          data: res
        }
      } else {
        return {
          ...responseStatus(2)
        }
      }
    }
  },
  {
    url: '/api/movie/movieUpdate',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(movieFile)
      body.updateTime = dayjs().format('YYYY-MM-DD hh:mm:ss')
      updateOneData(data, body)
      setFileContext(movieFile, data)
      return {
        ...responseStatus()
      }
    }
  },
  // 电影删除
  {
    url: '/api/movie/movieRemove',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(movieFile)
      body.id.forEach(id => {
        removeOneData(data, id)
      });
      setFileContext(movieFile, data)
      return {
        ...responseStatus()
      }
    }
  },
  // 获取地区
  {
    url: '/api/movie/movieAreaList',
    method: 'get',
    timeout: 200,
    response: ({ query }) => {
      const res = getFileContext(movieAreaFile)
      return {
        ...responseStatus(),
        data: res.reverse(),
        total: res.length
      }
    }
  },
  // 地区新增
  {
    url: '/api/movie/movieAreaAdd',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(movieAreaFile)
      const res = getDataByKey(data, body.name, 'name')
      if (res) {
        return {
          ...responseStatus(2),
          msg: '地区已存在'
        }
      }
      body.createTime = dayjs().format('YYYY-MM-DD hh:mm:ss')
      addOneData(data, body)
      setFileContext(movieAreaFile, data)
      return {
        ...responseStatus()
      }
    }
  },
  // 地区删除
  {
    url: '/api/movie/movieAreaRemove',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(movieAreaFile)
      removeOneData(data, body.id)
      setFileContext(movieAreaFile, data)
      return {
        ...responseStatus()
      }
    }
  },
  // 获取类型
  {
    url: '/api/movie/movieTypeList',
    method: 'get',
    timeout: 200,
    response: ({ query }) => {
      const res = getFileContext(movieTypeFile)
      return {
        ...responseStatus(),
        data: res.reverse(),
        total: res.length
      }
    }
  },
  // 类型新增
  {
    url: '/api/movie/movieTypeAdd',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(movieTypeFile)
      const res = getDataByKey(data, body.name, 'name')
      if (res) {
        return {
          ...responseStatus(2),
          msg: '类型已存在'
        }
      }
      body.createTime = dayjs().format('YYYY-MM-DD hh:mm:ss')
      addOneData(data, body)
      setFileContext(movieTypeFile, data)
      return {
        ...responseStatus()
      }
    }
  },
  // 类型删除
  {
    url: '/api/movie/movieTypeRemove',
    method: 'post',
    timeout: 200,
    response: ({ body }) => {
      const data = getFileContext(movieTypeFile)
      removeOneData(data, body.id)
      setFileContext(movieTypeFile, data)
      return {
        ...responseStatus()
      }
    }
  }
]
